package cn.edu.neu.lab603.des

import java.io.PrintStream

import cn.edu.neu.lab603.cloudeval.CloudEval
import cn.edu.neu.lab603.cloudeval.entity.DataCenter

import scala.collection.immutable.ListMap

/** 日志记录收集器。
  *
  * @param fileName 输出的文件名
  * @param metrics  监视器列表
  */
class MetricsLogMonitor(val fileName: String,
                        val metrics: scala.collection.immutable.ListMap[String, SimMetric[Any]])
  extends SimMonitor[CloudEval] {

  // 输出流
  private val out = new PrintStream(fileName)

  /** 利用 Seq 来初始化日志记录收集器。
    *
    * @param fileName 输出的文件名
    * @param metrics  监视器列表
    * @return 日志记录收集器
    */
  def this(fileName: String, metrics: Seq[(String, SimMetric[Any])]) = this(fileName, ListMap.empty ++ metrics)

  // 输出表头
  out.print("clock")
  for ((key, _) <- metrics) {
    out.print(s",$key")
  }
  out.println()

  override def isMatch(context: CloudEval): Boolean = context.getCurrentTask.isInstanceOf[DataCenter.UpdateEvent]

  override def before(context: CloudEval): Unit = {}

  override def after(context: CloudEval): Unit = {
    out.print(s"${context.getCurrentClock}")
    for ((_, monitor) <- metrics) {
      out.print(s",${monitor.getCurrentValue}")
    }
    out.println()
  }

}